# 引用GRPC通信相关的包
import grpc
import json
from pprint import pprint
import os
import sys
sys.path.insert(0, sys.path[0]+"/../")
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
# 引用与仿真系统交互的GRPC通信协议
from sh15.simulation import simserver_pb2
from sh15.simulation import simserver_pb2_grpc

_HOST = '127.0.0.1'# ip地址固定为本地ip
_PORT = '6000' # 端口号固定为6000
channel = grpc.insecure_channel("{0}:{1}".format(_HOST, _PORT))
client = simserver_pb2_grpc.GreeterStub(channel=channel)
user_name = "admin" # user_name固定为admin

# 算法与仿真系统的交互接口
msg = json.dumps({
        "source":"test",
        "ip":"test",
        "user_name": user_name,
        "engine_name": "",
        "flag":"simulation",
        "func_name":"get_state", # 接口函数名
        "kwargs":{} # 接口函数的参数
    })
request = client.control(simserver_pb2.MsgStr(msg=msg)) # request为接口函数的返回值
print(request.msg)
data = json.loads(request.data) 
pprint(data)

# msg = json.dumps({
#     "source":"test",
#     "ip":"test",
#     "user_name": user_name,
#     "engine_name": "",
#     "flag":"simulation",
#     "func_name":"send_command", # 接口函数名
#     "kwargs" :{"cmd":{
#         "unit_name": str,
#         "target_speed": float,
#         "target_course": float
#         }}
#     })
# msg = json.dumps({
#     "source":"test",
#     "ip":"test",
#     "user_name": user_name,
#     "engine_name":"",
#     "flag":"simulation",
#     "func_name":"cmd_lock", # 接口函数名
#     "kwargs":{"unit1_name":str,"unit2_name":str}
#     })
